﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using HCommon;
using System.Data;
namespace eStock.hControl
{
    public partial class hTextBox : TextBox
    {
        public hTextBox()
        {
            InitializeComponent();
        }

        public hTextBox(IContainer container)
        {
            container.Add(this);
            InitializeComponent();

            ttStatus = new ToolTip();
            ttStatus.IsBalloon = true;
        }

        public ToolTip ttStatus;

        protected bool hNotEmpty = false;
        [Category("1-HCOM"), DefaultValue(false), Description("Kiểm tra trống")]
        public bool NotEmpty
        {
            get { return hNotEmpty; }
            set { hNotEmpty = value; }
        }

        protected bool hCheckEmail = false;
        [Category("1-HCOM"), DefaultValue(false), Description("Kiểm tra email có hợp lệ hay không")]
        public bool CheckEmail
        {
            get { return hCheckEmail; }
            set { hCheckEmail = value; }
        }

        protected string hFBCode = string.Empty;
        [Category("1-HCOM"), DefaultValue(""), Description("Mã Find Box")]
        public string FBCode
        {
            get { return hFBCode; }
            set
            {
                if (value == null)
                {
                    return;
                }
                if (value.Trim().Length == 0)
                {
                    this.BackColor = SystemColors.Window;
                    hFBCode = "";
                }
                else
                {
                    this.BackColor = Color.PaleTurquoise;
                    hFBCode = value;
                }
            }
        }

        protected TextBox hFBCodeName;
        [Category("1-HCOM"), DefaultValue(false), Description("Name của item trong Find Box")]
        public TextBox FBCodeName
        {
            get { return hFBCodeName; }
            set { hFBCodeName = value; }
        }


        private void hTextBox_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (hFBCode.Trim().Length > 0)
                {
                    DataTable dt = new DataTable();
                    dt = Dao.Get_Table("Exec spLoadFB '" + Common.xoaNhay(hFBCode) + "',N'" + Common.xoaNhay(this.Text) + "'");

                    hFindBox findBox = new hFindBox(dt, hFBCode, this.Text);
                    findBox.ShowDialog();

                    try // Nếu gán được vào text luôn thì tức là chỉ có 1 dòng
                    {
                        this.Text = findBox.gRow.Cells["MinorCd"].Value.ToString();
                        if (this.hFBCodeName != null)
                        {
                            this.hFBCodeName.Text = findBox.gRow.Cells["MinorName"].Value.ToString();
                        }
                        return;
                    }
                    catch { }
                }
            }
            catch { }
        }

        private void hTextBox_Leave(object sender, EventArgs e)
        {
            try
            {
                // Nếu text bằng rồng thì return luôn
                if (this.Text.Trim().Length == 0)
                {
                    if (this.hFBCodeName != null)
                    {
                        this.hFBCodeName.Text = string.Empty;
                    }
                    return;
                }

                if (FBCode.Trim().Length > 0)
                {
                    DataTable dt = new DataTable();
                    dt = Dao.Get_Table("Exec spLoadFB '" + Common.xoaNhay(hFBCode) + "',N'" + Common.xoaNhay(this.Text) + "'");
                    if (dt.Rows.Count == 1)
                    {
                        this.Text = dt.Rows[0]["MinorCd"].ToString();
                        if (this.hFBCodeName != null)
                        {
                            this.hFBCodeName.Text = dt.Rows[0]["MinorName"].ToString();
                        }
                        return;
                    }

                    hFindBox findBox = new hFindBox(dt, hFBCode, this.Text);
                    findBox.ShowDialog();

                    try // Nếu gán được vào text luôn thì tức là chỉ có 1 dòng
                    {
                        this.Text = findBox.gRow.Cells["MinorCd"].Value.ToString();
                        if (this.hFBCodeName != null)
                        {
                            this.hFBCodeName.Text = findBox.gRow.Cells["MinorName"].Value.ToString();
                        }
                        return;
                    }
                    catch { }
                }
            }
            catch (Exception ex) { }
        }
    }
}
